package action.util;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseHelper extends SQLiteOpenHelper {
// setting layout
	public static final String DATABASE_NAME = "new_elifile3.db";
	public static final int DATABASE_VERSION = 1;
	public static final String TABLE_NAME = "users";
	public static final String ID = "id";
	public static final String DISPLAY = "display";
	public static final String BACKGROUND = "background";
	public static final String LANGUAGE = "language";
	public static final String ICON = "icon";
	
// favorite local
	public static final String TABLE_FAVORITE= "favorite";
	public static final String COLUMN_ID = "id";
	public static final String COLUMN_NAME = "name";
	public static final String COLUMN_FOLDER = "folder";
	public static final String COLUMN_PATH = "path";

// create server ftp	
	public static final String TABLE_FTP = "ftpconnect";
	public static final String COLUMN_ID_FTP = "id";
	public static final String COLUMN_HOSTNAME = "ftp_hostname";
	public static final String COLUMN_PORT = "ftp_port";
	public static final String COLUMN_USERNAME = "ftp_username";
	public static final String COLUMN_PASSWORD = "ftp_password";

	// favorite ftp
	public static final String TABLE_FAVORITE_FTP= "favoriteftp";
	public static final String COLUMN_ID_FAVORITE_FTP = "id";
	public static final String COLUMN_NAME_FAVORITE_FTP = "name";
	public static final String COLUMN_FOLDER_FAVORITE_FTP = "folder";
	public static final String COLUMN_PATH_FAVORITE_FTP = "path"; 
	public static final String COLUMN_HOSTNAME_FAVORITE = "ftp_hostname_favo";
	public static final String COLUMN_PORT_FAVORITE= "ftp_port_favo";
	public static final String COLUMN_USERNAME_FAVORITE_FTP="ftp_username_favo";
	public static final String COLUMN_PASSWORD_FAVORITE_FTP ="ftp_password_favo";
	
	// create server LAN
	public static final String TABLE_LAN_SERVER = "lanserver";
	public static final String COLUMN_ID_LAN = "id";
	public static final String COLUMN_NAME_SERVER_LAN ="name_lan";
	public static final String COLUMN_USERNAME_LAN ="username_lan";
	public static final String COLUMN_PASSWORD_LAN = "password_lan";
	

	public DatabaseHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		String queryUser = "create table " + TABLE_NAME + " (" + ID
		+ " INTEGER PRIMARY KEY, " + DISPLAY + " INTEGER, "
		+ BACKGROUND + " INTEGER ," + LANGUAGE + " INTEGER," + ICON
		+ " INTEGER)";

		String queryFavorite = "create table " + TABLE_FAVORITE + " (" + COLUMN_ID
		+ " INTEGER PRIMARY KEY, " + COLUMN_NAME + " text not null, "
		+ COLUMN_PATH + " text not null,  "
		+ COLUMN_FOLDER + " INTEGER not null)";

		String queryFTP = "create table "
			+ TABLE_FTP + "( " + COLUMN_ID_FTP + " integer primary key autoincrement, " + COLUMN_HOSTNAME
			+ " text not null , " + COLUMN_PORT + " INTEGER not null, "+ COLUMN_USERNAME + " text not null, " + COLUMN_PASSWORD + " text not null );";

		String queryFavoriteFTP = "create table " + TABLE_FAVORITE_FTP + " (" + COLUMN_ID_FAVORITE_FTP
		+ " INTEGER PRIMARY KEY autoincrement, " + COLUMN_NAME_FAVORITE_FTP + " text, "
		+ COLUMN_PATH_FAVORITE_FTP + " text,  "
		+ COLUMN_FOLDER_FAVORITE_FTP + " INTEGER, "
		+ COLUMN_HOSTNAME_FAVORITE + " text, "
		+ COLUMN_PORT_FAVORITE + " INTEGER, " 
		+ COLUMN_USERNAME_FAVORITE_FTP + " text, "
		+ COLUMN_PASSWORD_FAVORITE_FTP + " text)";
		
		String queryLan = " create table " + TABLE_LAN_SERVER + "( " + COLUMN_ID_LAN + " integer primary key autoincrement, " + COLUMN_NAME_SERVER_LAN
		+ " text not null , " + COLUMN_USERNAME_LAN + " text not null, " + COLUMN_PASSWORD_LAN + " text not null );" ;

		db.execSQL(queryUser);
		db.execSQL(queryFavorite);
		db.execSQL(queryFTP);
		db.execSQL(queryFavoriteFTP);
		db.execSQL(queryLan);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		//		String query = "drop table if exists " + TABLE_NAME;
		//		db.execSQL(query);
		Log.w(DatabaseHelper.class.getName(),
				"Upgrading database from version " + oldVersion + " to "
				+ newVersion + ", which will destroy all old data");
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_FAVORITE);
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_FTP);
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_FAVORITE_FTP);
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_LAN_SERVER);
		onCreate(db);
	}


}
